<template>
  <div>
    <div class="mb-3">
      <dmb-form
        ref="form"
        :form-items="rebase"
        :model-value="propsSearchForm"
        :item-style="formItemStyle"
        @update="(row) => (propsSearchForm = row)"
      >
        <template v-slot:header="row" class="flex flex-row-reverse">
          <div class="mb-3 flex flex-row-reverse">
            <div>
              <el-button type="success" @click="$refs.table.getListData()"
                >查询</el-button
              >
              <el-button type="danger" @click="importFile">导入</el-button>
              <!-- 不显示 文件上传的图标 -->
              <input
                type="file"
                accept=".xls,.xlsx"
                ref="inputfile"
                @change="changeExcel"
                class="hidden"
              />
              <el-button type="success" @click="$refs.table.saveToUpdate()"
                >保存</el-button
              >
              <el-button type="primary" @click="downLoad">导出</el-button>
            </div>
          </div>
        </template>
      </dmb-form>
    </div>
    <div>
      <dmb-table
        ref="table"
        :prop-list="tableBase()"
        :is-operate="true"
        :show-index-column="true"
        select-column-type="CHECKBOX"
        :props-search-form="propsSearchForm"
        :req-request="requestConfig"
        :isSaveToUpadate="true"
        :saveRequest="saveRequest"
        select-key="id"
        :cell-style="cellStyle"
        :header-cell-style="headerCellStyle"
        :initialize="false"
        :change-search-form="false"
        :show-footer="true"
        :form-item-style="tableFormStyle"
        @selectionChange="
          (row) => {
            selectionArr = row;
          }
        "
      >
        <template v-slot:operate="{ row }">
          <el-button type="danger" v-if="row.id" @click="del(row.id)"
            >删除</el-button
          >
        </template>
        <template v-slot:status="{ row }">
          <el-checkbox v-model="row.status" :true-label="1" :false-label="0">
          </el-checkbox>
        </template>
      </dmb-table>
    </div>
  </div>
</template>
      </dmb-table>
    </div>
  </div>
</template>
<script>
import dmbForm from "@/components/DmbForm";
import dmbTable from "@/components/LTable/index.vue";

import {
  formBase,
  tableBase,
  cellStyle,
  headerCellStyle,
  tableFormStyle,
  formItemStyle,
} from "./config";

import { download } from "@/utils/request";
import { carrier_ListAPI } from "@/api/data/carrier.js";
import {
  timely_maintenance_delAPI,
  timely_maintenance_ImportAPI,
} from "@/api/data/time-effect-relationship.js";
import dayjs from "dayjs";

export default {
  name: "time-effect-relationship",
  data() {
    return {
      rebase: formBase,
      propsSearchForm: {},
      requestConfig: {
        url: "/base/manage/timelyRelationship/list",
        method: "get",
        montage: true,
      },
      saveRequest: {
        url: "/base/manage/timelyRelationship/save",
        method: "post",
        montage: false,
      },
      selectionArr: [], // 多选框内容
    };
  },
  methods: {
    tableBase() {
      return tableBase;
    },
    formBase() {
      return formBase;
    },
    // 导出
    async downLoad() {
      if (this.selectionArr.length > 0) {
        const ids = this.selectionArr.map((v) => v.id);
        this.download(
          "/base/manage/timelyRelationship/export",
          {
            ids,
          },
          `时效关系_${dayjs().format("YYYY/MM/DD hh:mm:ss")}.xlsx`
        );
      } else {
        this.$message.error("请选择条数据导出");
      }
    },
    // 多选框内容
    selectionChange(data) {
      this.selectionArr = data;
    },
    async del(id) {
      await this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      });
      await timely_maintenance_delAPI(id);
      this.$refs.table.getListData();
      this.$message.success("删除成功");
    },
    // 导入
    importFile() {
      this.$refs.inputfile.click();
    },
    changeExcel(e) {
      const that = this;
      // 错误情况判断
      const files = e.target.files[0];
      console.log(files);
      if (files.length <= 0) {
        return false;
      }
      let data = new FormData();
      data.append("file", files);
      console.dir(data, 1);
      timely_maintenance_ImportAPI(data).then((res) => {
        this.$message.success("导入成功");
      });
    },
  },
  computed: {
    formItemStyle() {
      return formItemStyle;
    },
    tableFormStyle() {
      return tableFormStyle;
    },
    cellStyle() {
      return cellStyle;
    },
    headerCellStyle() {
      return headerCellStyle;
    },
  },
  components: { dmbTable, dmbForm },
  mounted() {
    // 承运商
    carrier_ListAPI()
      .then((res) => {
        this.rebase[2].options = res.rows;
      })
      .catch((err) => {});
  },
  watch: {
    propsSearchForm: {
      deep: true,
      handler: function (value) {
        this.tableSearchForm = { ...value };
        delete this.tableSearchForm.shelfTime;

        // 时效
        if (value.warehouseName) {
          const obj = store.state.custom_dict.warehouse.find((item) => {
            return item.id == value.warehouseName;
          });
          this.tableSearchForm.warehouseName = obj ? obj.warehouseName : "";
        }
      },
    },
  },
  beforeDestroy() {
    this.$store.dispatch("custom_dict/timely");
  },
};
</script>

<style lang="scss" scoped>
</style>

